﻿Public Class LapPhieuThuForm
    Dim dt As DataTable
    Dim dt_thunhap As DataTable
    Dim dt_nganhang As DataTable
    Dim MaPhieuThu As Integer
    Dim MaUserThu As Integer
    Sub New(Optional ByVal MaPhieuThu As Integer = 0)

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.        
        Me.MaPhieuThu = MaPhieuThu
        LoadThuNhap()
        LoadTaiKhoanNganHang()
        If MaPhieuThu <> 0 Then
            dt = QuanLyThuBUS.GetAllByMa(MaPhieuThu)
            If dt.Rows.Count > 0 Then
                txtMaPhieuThu.Text = MaPhieuThu
                txtChiTiet.Text = dt.Rows(0)("ChiTiet")
                txtSoTien.Text = dt.Rows(0)("SoTien")
                txtMaUserThu.Text = dt.Rows(0)("fullname")
                MaUserThu = dt.Rows(0)("MaUserThu")
                dtNgay.Text = dt.Rows(0)("Ngay")
                cbMaNganHang.SelectedValue = dt.Rows(0)("MaNganHang")
                cbMaThuNhap.SelectedValue = dt.Rows(0)("MaThuNhap")
            End If
        End If        
    End Sub
    Private Sub LoadThuNhap()
        dt_thunhap = ThuNhapDAO.GetAll()
        cbMaThuNhap.DataSource = dt_thunhap
        cbMaThuNhap.DisplayMember = "Ten"
        cbMaThuNhap.ValueMember = "MaThuNhap"
    End Sub
    Private Sub LoadTaiKhoanNganHang()
        dt_nganhang = TaiKhoanNganHangDAO.GetAll(MainForm.current_user.MaUser)
        cbMaNganHang.DataSource = dt_nganhang
        cbMaNganHang.DisplayMember = "Ten"
        cbMaNganHang.ValueMember = "MaNganHang"
    End Sub
    Private Sub btSave_Click(sender As System.Object, e As System.EventArgs) Handles btSave.Click
        If txtSoTien.Text.Trim().Length = 0 Or Not IsNumeric(txtSoTien.Text) Then
            MessageBox.Show("Xin vui lòng nhập vào số tiền")
            Return
        End If
        If txtChiTiet.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng nhập vào chi tiết")
            Return
        End If
        If txtMaUserThu.Text.Trim().Length = 0 Then
            MessageBox.Show("Xin vui lòng chọn người thu")
            Return
        End If
        If cbMaNganHang.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn tài khoản")
            Return
        End If
        If cbMaThuNhap.SelectedIndex < 0 Then
            MessageBox.Show("Xin vui lòng chọn loại thu nhập")
            Return
        End If
        Dim obj As New PhieuThu
        With obj
            .Ngay = dtNgay.Text
            .MaNganHang = Integer.Parse(cbMaNganHang.SelectedValue)
            .MaUserThu = MaUserThu
            .MaUserLap = MainForm.current_user.MaUser
            .SoTien = Double.Parse(txtSoTien.Text)
            .MaThuNhap = Integer.Parse(cbMaThuNhap.SelectedValue)
            .ChiTiet = txtChiTiet.Text
        End With
        If QuanLyThuBUS.Write(obj, MaPhieuThu) Then
            MessageBox.Show("Lưu thành công")
            Me.DialogResult = Windows.Forms.DialogResult.OK
        End If
    End Sub

    Private Sub btTim_Click(sender As System.Object, e As System.EventArgs) Handles btTim.Click
        Dim frm As New SearchUsersForm
        If frm.ShowDialog() = Windows.Forms.DialogResult.OK Then            
            txtMaUserThu.Text = frm.selected_user.Fullname
            MaUserThu = frm.selected_user.MaUser
        End If
    End Sub

    Private Sub btExit_Click(sender As System.Object, e As System.EventArgs) Handles btExit.Click
        Me.Dispose()
    End Sub

    Private Sub btnAddThuNhap_Click(sender As System.Object, e As System.EventArgs) Handles btnAddThuNhap.Click
        Dim frm As New ThuNhapForm
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadThuNhap()
        End If
    End Sub

    Private Sub btnAddTaiKhoan_Click(sender As System.Object, e As System.EventArgs) Handles btnAddTaiKhoan.Click
        Dim frm As New TaiKhoanNganHangForm
        If frm.ShowDialog = Windows.Forms.DialogResult.OK Then
            LoadTaiKhoanNganHang()
        End If
    End Sub
End Class